package gui.popupmenu;

import gui.form.MainForm;
import gui.image.ImagePanel;
import java.awt.event.MouseAdapter;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import org.ini4j.Profile;
import qviewer.ServiceManager;
import util.MyIniSection;

/**
 *
 * @author Quân
 */
public class FolderImagePanelPopup extends ImagePanelPopup {

    public FolderImagePanelPopup() {
        JMenuItem addToAlbum = new JMenu("Add to album");

        try {
            List<MyIniSection> mises =
                    ServiceManager.getInstance().getImportManager().getAlbums();
            for (MyIniSection mis : mises) {
                Profile.Section section = mis.getSection();
                JMenuItem item = new JMenuItem(section.get(MyIniSection.NodeName));
                item.addMouseListener(new AddToAlbumMouseAdaptor(imagePanel, mis.getImageList()));
                addToAlbum.add(item);
            }
            JMenuItem item = new JMenuItem("New album...");
            addToAlbum.add(item);
        } catch (Exception ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }

        addToAlbum.addMouseListener(new MouseAdapter() {
        });
        add(addToAlbum);
    }

    public FolderImagePanelPopup(ImagePanel imagePanel) {
        this.imagePanel = imagePanel;
        JMenuItem addToAlbum = new JMenu("Add to album");
        try {
            List<MyIniSection> mises =
                    ServiceManager.getInstance().getImportManager().getAlbums();
            for (MyIniSection mis : mises) {
                Profile.Section section = mis.getSection();
                File file = new File(section.get(MyIniSection.NodePath));
                String filePath = imagePanel.getFile().getPath();
                JMenuItem item = new JMenuItem(section.get(MyIniSection.NodeName));
                if (!util.Function.checkIfFileContentLine(file, filePath)) {
                    item.addMouseListener(new AddToAlbumMouseAdaptor(imagePanel, mis.getImageList()));
                } else {
                    item.setEnabled(false);
                }
                addToAlbum.add(item);
            }
            JMenuItem item = new JMenuItem("New album...");
            addToAlbum.add(item);
        } catch (Exception ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        add(addToAlbum);

        JMenuItem copyToFolder = new JMenuItem("Copy");
        copyToFolder.addMouseListener(new CopyMouseAdaptor(imagePanel));
        add(copyToFolder);

        JMenuItem moveToFolder = new JMenuItem("Cut");
        moveToFolder.addMouseListener(new CutMouseAdaptor(imagePanel));
        add(moveToFolder);

        JMenuItem delete = new JMenuItem("Delete");
        delete.addMouseListener(new DeleteMouseAdaptor(imagePanel));
        add(delete);
    }
}
